﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using Xpedite.Localization;
using Xpedite.Resources.Definitions;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Resources.Combiners
{
  internal class SpriteCombiner : SpriteCombinerBase
  {
    private readonly ImageFormat _imageFormat;
    private readonly PixelFormat _pixelFormat;

    public ImageFormat ImageFormat { get { return _imageFormat; } }
    public PixelFormat PixelFormat { get { return _pixelFormat; } }

    public SpriteCombiner(ImageFormat imageFormat, PixelFormat pixelFormat)
    {
      Verify.NotNull(imageFormat, "imageFormat");

      _imageFormat = imageFormat;
      _pixelFormat = pixelFormat;
    }

    protected override IResource BuildResource(IResourceUrl virtualUrl, IResourceDefinition definition, ISpriteLayout spriteLayout)
    {
      if (!definition.Resources.Select(item => item.Resource).OfType<IImageResource>().Any())
        throw new XpediteException(String.Format(ExceptionMessages.AtLeastOneResourceRequired, definition.ReferenceName));

      return new Resource
      {
        Bytes = GenerateSprite(spriteLayout, _pixelFormat, _imageFormat),
        Compressable = false,
        Type = definition.ResourceType,
        Encoding = Encoding.Default,
        LastModified = definition.Resources.Select(item => item.Resource).OfType<IImageResource>().Min(resource => resource.LastModified)
      };
    }

    public static Byte[] GenerateSprite(ISpriteLayout layout, PixelFormat pixelFormat, ImageFormat imageFormat)
    {
      using (var compoundImage = new Bitmap(layout.Width, layout.Height, pixelFormat))
      using (var g = Graphics.FromImage(compoundImage))
      {
        foreach (var image in layout.Images)
          g.DrawImage(image.Image, image.Position);

        using (var memoryStream = new MemoryStream())
        {
          compoundImage.Save(memoryStream, imageFormat);
          return memoryStream.ToArray();
        }
      }
    }
  }
}
